/**
 * Created with IntelliJ IDEA
 * Description:
 * User: SEKI TOMOKI
 * Date: 2024-07-16
 * Time: 13:37
 */
public class MySingleLinkedList implements IList{

    static class ListNode {
        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode head;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        //检查下标是否合法
        if(index < 0 || index > size()) {
            throw new IndexIllegalException("下标不合法");
        }

        //如果要插到第一个节点,就用头插法
        //如果要插到最后一个节点,就用尾插法
        //否则就要插到中间
        if(index == 0) {
            addFirst(data);
        } else if (index == size()) {
            addLast(data);
        }else {
            ListNode node = new ListNode(data);
            ListNode cur = searchPrev(index);
            node.next = cur.next;
            cur.next = node;
        }
    }

    /**
     * searchPrev方法用于找到该下标位置的前一个节点
     * @param index
     * @return
     */
    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        while(count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head.val == key) {//要考虑删除第一个节点的情况
            this.head = head.next;
        }else {
            //找出存放 key 的节点的前一个节点
            ListNode prev = findPrev(key);
            //检查返回值是否为 null
            if(prev == null) {
                System.out.println("没有你要删除的数字");
                return ;
            }
            //删除
            ListNode del = prev.next;
            prev.next = del.next;
        }
    }

    /**
     * findPrev方法用来查找 存放 key 的节点的前一个节点
     * @param key
     * @return
     */
    private ListNode findPrev(int key) {
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }

        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }

        if(this.head.val == key) {//要考虑删除第一个节点的情况
            this.head = this.head.next;
            return;
        }
    }

    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.val = 0;//如果 val 域是引用数据类型要改为 null
            cur.next = null;
            cur = curNext;
        }
        //此时 head指向的节点还没置空
        this.head = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

}
